package www.ym.com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.ym.com.common.CustomException;
import www.ym.com.entity.Department;
import www.ym.com.entity.Note;
import www.ym.com.entity.Role;
import www.ym.com.entity.User;
import www.ym.com.mapper.RoleMapper;
import www.ym.com.service.DepartmentService;
import www.ym.com.service.RoleService;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 增加角色信息
     *
     * @param role
     */
    @Override
    public void addRole(Role role) {
        role.setStatus(0);
        role.setCreateTime(LocalDateTime.now());

        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getDepartmentName, role.getDepartmentName());
        Department department = departmentService.getOne(wrapper);
        Long departmentId = department.getId();
        role.setDepartmentId(departmentId);

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleName, role.getRoleName());
        int count = roleService.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("角色名称已使用");
        }
        roleService.save(role);
    }

    /**
     * 修改角色信息
     *
     * @param role
     */
    @Override
    public void updateRole(Role role) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleName, role.getRoleName());
        List<Role> list = roleService.list(queryWrapper);
        if (list != null) {
            for (Role data : list) {
                Long id = data.getId();
                String roleName = data.getRoleName();
                if (!id.equals(role.getId()) && roleName.equals(role.getRoleName())) {
                    throw new CustomException("角色名称已使用");
                }
            }
        }
        roleService.updateById(role);
    }

    /**
     * 分页
     *
     * @param jsonObject
     * @return
     */
    @Override
    public Page<Role> pageUser(JSONObject jsonObject) {
        //当前页数
        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));
        //每页显示数
        Long pageSize = Long.valueOf(String.valueOf(jsonObject.get("pageSize")));
        //角色名称
        String roleName = String.valueOf(jsonObject.get("roleName"));
        //部门名称
        String departmentName = String.valueOf(jsonObject.get("departmentName"));
        log.info("当前页为" + pageNum);
        log.info("每页显示数" + pageSize);
        log.info("角色名称" + roleName);

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(roleName != "", Role::getRoleName, roleName).or().eq(departmentName != "", Role::getDepartmentName, departmentName);

//                .notIn(Role::getRoleName, "超级管理员", "管理员");
        wrapper.orderByDesc(Role::getUpdateTime);

        //进行分页查询-可跟条件
        Page<Role> urf = roleMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        System.out.println("数据为：" + urf.getRecords());
        System.out.println("总数为：" + urf.getTotal() + ",总页数为：" + urf.getPages());
        System.out.println("当前页为：" + urf.getCurrent() + ",每页限制：" + urf.getSize());
        return urf;
    }
}
